package com.tim.framework.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;


public class UrlTools {
	private static ResourceBundle urls;
	static {
		urls = ResourceBundle.getBundle("urls");
	}

	private static String WEBSITE_ID;

	static boolean initialized = false;

	/**
	 * 初始化UrlTools,必须提供分配当前网站的websiteId
	 *
	 * @param websiteId
	 */
	public static void init(String websiteId) {
		WEBSITE_ID = websiteId;
		initialized = true;
	}

	/**
	 * 用户登录页面的地址，附加了登陆后应该返回的地址
	 *
	 * @param returnUrl
	 *            应该返回的地址，会被URLEncode
	 * @param msg
	 *            TODO
	 *
	 * @return 登陆连接
	 */
	public static String SignIn(String returnUrl, String msg) {
		assertInitialized();
		String encodedUrl = returnUrl;
		try {
			encodedUrl = URLEncoder.encode(returnUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		try {
			msg = URLEncoder.encode(msg, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String signInUrl = MessageFormat.format(urls.getString("sign_in"),
				new Object[] { encodedUrl, msg, WEBSITE_ID });
		return signInUrl;
	}

	/**
	 * 注册连接
	 *
	 * @param ret
	 * @param returnUrl
	 * @return 注册连接
	 */
	public static String Register(String returnUrl) {
		assertInitialized();
		String encodedUrl = returnUrl;
		try {
			encodedUrl = URLEncoder.encode(returnUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String registerUrl = MessageFormat.format(urls.getString("register"),
				new Object[] { encodedUrl, WEBSITE_ID });
		return registerUrl;
	}

	/**
	 * 登出连接
	 *
	 * @param ret
	 * @param returnUrl
	 * @return 登出连接
	 */
	public static String Logout(String returnUrl) {
		assertInitialized();
		String encodedUrl = returnUrl;
		try {
			encodedUrl = URLEncoder.encode(returnUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String logoutUrl = MessageFormat.format(urls.getString("logout"),
				new Object[] { encodedUrl, WEBSITE_ID });
		return logoutUrl;
	}

	/**
	 * 登出连接
	 *
	 * @param ret
	 * @param returnUrl
	 * @return 登出连接
	 */
	public static String Denined(String returnUrl) {
		assertInitialized();
		String encodedUrl = returnUrl;
		try {
			encodedUrl = URLEncoder.encode(returnUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String denined = MessageFormat.format(urls.getString("denined"),
				new Object[] { encodedUrl, WEBSITE_ID });
		return denined;
	}

	/**
	 * 得到头像url, 头像会根据传入的宽度自动缩放
	 *
	 * @param jongoId
	 *            用户的jongo id
	 * @param width
	 *            头像宽度，最大100，最小1，不在该范围的都会被忽略，使用默认大小，即100
	 * @return 头像url
	 */
	public static String BuddyIcon(String jongoId, int width) {
		assertInitialized();
		String icon = MessageFormat.format(urls.getString("buddy_icon"),
				new Object[] { jongoId, new Integer(width) });
		return icon;
	}

	public static String Profile() {
		assertInitialized();
		return urls.getString("profile");
	}

	@SuppressWarnings("unchecked")
	public static String CurrentUrl(HttpServletRequest request) {
		StringBuffer url = request.getRequestURL();
		url.append("?");
		Enumeration enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String name = (String) enumeration.nextElement();

			String[] value = request.getParameterValues(name);
			if (value.length == 1) {
				try {
					url.append(name + "="
							+ URLEncoder.encode(value[0], "UTF-8") + "&");
				} catch (UnsupportedEncodingException e) {
					url.append(name + "=" + value[0] + "&");
				}
			} else {
				for (int i = 0; i < value.length; i++) {
					try {
						url.append(name + "="
								+ URLEncoder.encode(value[i], "UTF-8") + "&");
					} catch (UnsupportedEncodingException e) {
						url.append(name + "=" + value[i] + "&");
					}
				}
			}
		}
		url.deleteCharAt(url.length() - 1);
		return url.toString();
	}

	/**
	 * @param gsid
	 * @return
	 */
	public static String TokenByGSID(String gsid) {
		assertInitialized();
		String url = MessageFormat.format(urls.getString("token_by_gsid"),
				new Object[] { gsid, WEBSITE_ID });
		return url;
	}

	/**
	 *
	 */
	private static void assertInitialized() {
		if (!initialized) {
			throw new RuntimeException("UrlTools not initialized properly!");
		}
	}

	/**
	 * @param retUrl
	 *            TODO
	 * @return
	 */
	public static String GlobalInitUrl(String retUrl) {
		assertInitialized();
		String encodedUrl = retUrl;
		try {
			encodedUrl = URLEncoder.encode(retUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String url = MessageFormat.format(urls.getString("global_init_url"),
				new Object[] { encodedUrl, WEBSITE_ID });
		return url;
	}
	/**
	 * 取回密码的连接
	 * @param returnUrl
	 * @return
	 */
	public static String GetPassword(String returnUrl) {
		assertInitialized();
		String encodedUrl = returnUrl;
		try {
			encodedUrl = URLEncoder.encode(returnUrl, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			// DO NOTHING
		}
		String logoutUrl = MessageFormat.format(urls.getString("get_password"),
				new Object[] { encodedUrl, WEBSITE_ID });
		return logoutUrl;
	}
}

